<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class TabController extends Controller
{
    //选项卡列表


    public function getIndex(Request $request)

    public function getIndex()


    public function getIndex(Request $request)
    {
        $min = DB::table('inmod')->min('id');
        $tabs = DB::table('inmod')->where('pid',$min)->get();
        // var_dump($id);
        $data = DB::table('inmod')->where('pid',0)->get();
        //解析模块
        return view('Admin.tab.index',['data'=>$data,'tabs'=>$tabs]);
    }

    public function getShow($id)
    {
       $tabs = DB::table('inmod')->where('pid',$id)->get();
       $data = DB::table('inmod')->where('pid',0)->get();
       return view('Admin.tab.index',['tabs'=>$tabs,'data'=>$data,]);
    }

    //选项添加
    public function getAdd()
    {
        //解析模块
        return view('Admin.tab.add');
    }

    //选项添加
    public function postInsert(Request $request)
    {
        //提取数据
        $data = $request->except('_token');
       //执行插入
        $res = DB::table('inmod')->insert($data);
        if($res)
        {

            return redirect('/Admin/tab/index')->with('success','分类添加成功');

            return redirect('/admin/tab/index')->with('success','分类添加成功');

            return redirect('/Admin/tab/index')->with('success','分类添加成功');
        }else{
            return back()->with('error','分类添加失败');
        }
    }

    //图片添加
    public function getPicadd()
    {

        $data = DB::table('inmod')->where('pid',0)->get();
        //解析模块
        return view('Admin.tab.picadd',['data'=>$data]);
    }

    //图片添加
    public function postPicinsert(Request $request)
    {
        $data = $request->except('_token');
        $count = DB::table('inmod')->where('pid',$data['pid'])->count();
        //dd($count);
        if($count >= 6){

            return back();

            return back()->with('error','最多添加六张图片');

            return back();
        }
        $data['logo'] = $this->upload($request);
        $res = DB::table('inmod')->insertGetId($data);
        if($res){
          return redirect('Admin/tab/index');
        }
    }

    //图片列表
    public function getPiclist()
    {
        $data = DB::table('inmod')->get();
        return view('Admin.tab.list',['data'=>$data]);

          return redirect('admin/tab/index');
        }
    }

    public function getList()
    {
        $min = DB::table('inmod')->min('id');
        $tabs = DB::table('inmod')->where('pid',$min)->get();
        // var_dump($id);
        $data = DB::table('inmod')->where('pid',0)->get();
        //解析模块
        return view('Admin.tab.list',['data'=>$data,'tabs'=>$tabs]);
    }

    //图片列表
    public function getPiclist($id)
    {

        $tabs = DB::table('inmod')->where('pid',$id)->get();
        $data = DB::table('inmod')->where('pid',0)->get();
        return view('Admin.tab.list',['data'=>$data,'tabs'=>$tabs]);

    }

    //图片修改
    public function getEdit($id)
    {
        $data = DB::table('inmod')->where('id',$id)->first();
        return view('Admin.tab.edit',['data'=>$data]);
    }

    //图片修改
    public function postUpdate(Request $request)
    {
        $data = $request->except('_token');
        if($request->hasFile('logo')){
           //调用方法进行头像的上传操作
           $data['logo'] = $this->upload($request);
        }

        $res = DB::table('inmod')->where('id',$data['id'])->updata($data);
        if($res){
            return redirect('Admin/tab/list')->with('success','用户修改成功');

        $res = DB::table('inmod')->where('id',$data['id'])->update($data);
        if($res){
            return redirect('admin/tab/piclist')->with('success','用户修改成功');

            return back()->with('error','用户修改失败');
        }
    }



    //删除数据
    public function getDelete($id)
    {
        $data = DB::table('inmod')->get();
        $del = DB::table('inmod')->where('id',$id)->delete();
        return view('Admin.tab.list',['data'=>$data]);
    }

    //文章列表
    public function getContent()
    {
        $data = DB::table('tabcontent')->leftjoin('inmod','tabcontent.tid','=','inmod.id')->get();
        return view('Admin.tab.content',['data'=>$data]);
    }

    //选项卡添加文章
    public function getConadd()
    {
        $data = DB::table('inmod')->get();
        return view('Admin.tab.conadd',['data'=>$data]);
    }

    //选项卡添加文章
    public function postConaddinsert(Request $request)
    {
        $data = $request->except('_token');
        $tca = DB::table('tabcontent')->where('tid',$data['tid'])->count();
        if($tca >= 1){
          return back()->with('error','一张图文下只能添加一篇文章');
        }
        $res = DB::table('tabcontent')->insertGetId($data);
        if($res){
            return redirect('admin/tab/content')->with('success','添加成功');
        }
    }

    //文章修改
    public function getConedit($ids)
    {
        $data = DB::table('tabcontent')->where('ids',$ids)->first();
        return view('Admin.tab.conedit',['data'=>$data]);
    }

    //文章修改
    public function postConupdate(Request $request)
    {
        $data = $request->except('_token');
        $res = DB::table('tabcontent')->where('ids',$data['ids'])->update($data);
        if($res){
            return redirect('admin/tab/content')->with('success','用户修改成功');
        $res = DB::table('inmod')->where('id',$data['id'])->updata($data);
        if($res){
            return redirect('Admin/tab/list')->with('success','用户修改成功');
        }else{
            return back()->with('error','用户修改失败');
        }
    }

    //文章删除
    public function getCondelete($ids)
    {
        $del = DB::table('tabcontent')->where('ids',$ids)->delete();
        return redirect('admin/tab/content')->with('success','删除成功');
    }

    //图片上传
    public function upload($request)
    {
        if ($request->hasfile('logo')) {
            $aa = $request->file('logo')->getClientMimeType();
            $type = array('image/png','image/gif','image/jpeg');
            if(!in_array($aa, $type)){
                die('请选择正确的文件类型');
            }
            $name = md5(time()+rand(1,999999));
            $su = $request->file('logo')->getClientOriginalExtension();
            $request->file('logo')->move('./public/Uploads',$name.'.'.$su);
            return '/public/Uploads/'.$name.'.'.$su;
        }
    }
}
